package cbbx_sm.parser;

import java.io.Serializable;

/**
 * This class represent a bounding box description.
 * 
 * x_ul = x upper left coordinate
 * x_br = x bottom right coordinate
 * blue = blue component
 * ...and so on.
 * 
 * @author Alessio Della Motta - University of California, Irvine
 */
public class BoundingBox implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private int x_ul, y_ul, x_br, y_br, blue, green, red;

	public BoundingBox(BoundingBox b){
		this(b.x_ul, b.y_ul, b.x_br, b.y_br, b.blue, b.green, b.red);
	}
	
	public BoundingBox(int x_ul, int y_ul, int x_br, int y_br){
		this(x_ul, y_ul, x_br, y_br, 0, 0, 0);
	}
	
	public BoundingBox(int x_ul, int y_ul, int x_br, int y_br,
					   int blue, int green, int red){
		this.blue = blue;
		this.green = green;
		this.red = red;
		this.x_ul = x_ul;
		this.y_ul = y_ul;
		this.x_br = x_br;
		this.y_br = y_br;
	}
	
	public int getX_ul() {
		return x_ul;
	}

	public int getY_ul() {
		return y_ul;
	}

	public int getX_br() {
		return x_br;
	}

	public int getY_br() {
		return y_br;
	}

	public int getBlue() {
		return blue;
	}

	public int getGreen() {
		return green;
	}

	public int getRed() {
		return red;
	}
	
	/**
	 * This method gets the area of the bounding box.
	 * @return area of the bbox.
	 */
	public int getArea(){
		return Math.abs(x_br - x_ul) * Math.abs(y_ul - y_br);
	}
	
	/**
	 * This method gets the perimeter of the bounding box.
	 * @return perimeeter of the bbox.
	 */
	public int getPerimeter(){
		return (Math.abs(x_br - x_ul) + Math.abs(y_ul - y_br)) * 2;
	}
	
	/**
	 * This method gets the x coordinate of bounding box's center.
	 * @return x coordinate of the bbox's center.
	 */
	public double getCenterX(){
		return ((double) (x_br - x_ul)) / 2 + x_ul;
	}
	
	/**
	 * This method gets the y coordinate of bounding box's center.
	 * @return y coordinate of the bbox's center.
	 */
	public double getCenterY(){
		return ((double) (y_ul - y_br)) / 2 + y_br;
	}
	
	public void setX_ul(int x_ul) {
		this.x_ul = x_ul;
	}

	public void setY_ul(int y_ul) {
		this.y_ul = y_ul;
	}

	public void setX_br(int x_br) {
		this.x_br = x_br;
	}

	public void setY_br(int y_br) {
		this.y_br = y_br;
	}

	public void setBlue(int blue) {
		this.blue = blue;
	}

	public void setGreen(int green) {
		this.green = green;
	}

	public void setRed(int red) {
		this.red = red;
	}

	@Override
	public String toString(){
		StringBuffer str = new StringBuffer();
		str.append("X_UL = " + x_ul + "; ");
		str.append("Y_UL = " + y_ul + "; ");
		str.append("X_BR = " + x_br + "; ");
		str.append("Y_BR = " + y_br + "; ");
		str.append("BLUE = " + blue + "; ");
		str.append("GREEN = " + green + "; ");
		str.append("RED = " + red + "; ");
		
		return str.toString();
	}
}
